import 'package:flutter/material.dart';
import 'package:helloworld/res/goalData.dart';
import 'package:helloworld/tabs.dart';
import 'package:flutter_cupertino_date_picker/flutter_cupertino_date_picker.dart';
import 'package:date_format/date_format.dart';
import 'package:dio/dio.dart';
import 'dart:ui';
class GoalDetail extends StatefulWidget{
  final arguments;
  GoalDetail({this.arguments});
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _GoalDetail(arguments: this.arguments);
  }
}
class _GoalDetail extends State<GoalDetail>{
  Color _textColor;
  DateTime _dateData;
  Color _finishedColor = Colors.white54;
  Color _unFinishColor = Colors.white54;
  var _date = TextEditingController();
  var _content = TextEditingController();
  var _contentData;
  var _oath = TextEditingController();
  var _oathData ;
  var _finishData;
  final arguments;
  _GoalDetail({this.arguments});
  Brightness brightnessValue;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    brightnessValue = window.platformBrightness;
    _dateData = null;
    _date.text = goalData[arguments['index']]['goaltime'];
    _contentData = goalData[arguments['index']]['goalcontent'];
    _content.text = goalData[arguments['index']]['goalcontent'];
    _oathData = goalData[arguments['index']]['goaloath'];
    _oath.text = goalData[arguments['index']]['goaloath'];
    _finishData = goalData[arguments['index']]['goalfinish'] ;
    if(goalData[arguments['index']]['goalfinish']=='yes'){
      _finishedColor = Colors.lightBlue;
    }else{
      _unFinishColor = Colors.lightBlue;
    }
    if(window.platformBrightness==Brightness.dark){
      _textColor = Colors.white54;
    }else{
      _textColor = Colors.black54;
    }
  }
  _delete(){
    showDialog(context: context,builder: (context){
      return AlertDialog(
        content: Text('您确定要删除吗?',style: TextStyle(fontSize: 20),),
        actions: <Widget>[
          FlatButton(
            child: Text('取消',style: TextStyle(fontSize: 20),),
            onPressed: (){
              Navigator.pop(context);
            },
          ),
          FlatButton(
            child: Text('确定',style: TextStyle(fontSize: 20),),
            onPressed: (){
              Navigator.pop(context);
              goalIndex = arguments['index'];
              Dio().post("http://123.207.114.127:1314/goal/deleteGoal",queryParameters: {
                'goalid':goalData[this.arguments['index']]['goalid']
              });
              Future.delayed(Duration(milliseconds: 50), (){
                Navigator.of(context).pop();
                Navigator.of(context).pushAndRemoveUntil(new MaterialPageRoute(builder: (context) => new Tabs( index:4)), (route) =>route==null);
              });
            },
          ),

        ],
      );
    });
  }
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        title: Text('编辑目标'),
        actions: <Widget>[
          InkWell(
            child: Padding(
              child: Text(
                '保存',
                style: TextStyle(color: Colors.white, fontSize: 20),
              ),
              padding: EdgeInsets.all(16),
            ),
            onTap: () {
              String time = '';
              if(_dateData==null){
                time = goalData[arguments['index']]['goaltime'];
              }else{
                time = '${formatDate(_dateData, [yyyy,'年',mm, '月',dd ,'日'])}';
              }
              Dio().post("http://123.207.114.127:1314/goal/updateGoal",queryParameters: {
                'goalid':goalData[this.arguments['index']]['goalid'],
                'goaltime':time,
                'goalcontent':_contentData,
                'goaloath':_oathData,
                'goalfinish':_finishData
              });
              Future.delayed(Duration(milliseconds: 50), (){
                Navigator.of(context).pop();
                Navigator.of(context).pushAndRemoveUntil(new MaterialPageRoute(builder: (context) => new Tabs( index:4)), (route) =>route==null);
              });
            },
          )
        ],
      ),
      body: Padding(
        padding: EdgeInsets.all(20),
        child: SingleChildScrollView(
          child: Column(
            children: <Widget>[
              InkWell(
                child: TextField(
                  controller: _date,
                  enabled: false,
                  decoration: InputDecoration(
                      border: OutlineInputBorder(), labelText: '预计日期'),
                ),
                onTap: () {
                  DatePicker.showDatePicker(
                    context,
                    onMonthChangeStartWithFirstDate: true,
                    pickerTheme: DateTimePickerTheme(
                      showTitle: true,
                      confirm: Text('确定',
                          style: TextStyle(color: Colors.black54, fontSize: 20)),
                      cancel: Text('取消',
                          style: TextStyle(color: Colors.black54, fontSize: 20)),
                    ),
                    minDateTime: DateTime.parse('2020-01-01'),
                    maxDateTime: DateTime.parse('2099-12-31'),
                    initialDateTime: DateTime.now(),
                    dateFormat: 'yyyy-MMMM-dd',
                    locale: DateTimePickerLocale.zh_cn,
                    onConfirm: (dateTime, List<int> index) {
                      setState(() {
                        _dateData = dateTime;
                        _date.text = '${formatDate(_dateData, [
                          yyyy,
                          '年',
                          mm,
                          '月',
                          dd,
                          '日'
                        ])}';
                      });
                    },
                  );
                },
              ),
              SizedBox(
                height: 10,
              ),
              TextField(
                controller: _content,
                keyboardAppearance: brightnessValue,
                decoration: InputDecoration(
                    border: OutlineInputBorder(), labelText: '目标内容'),
                onChanged: (value) {
                  setState(() {
                    _contentData = value;
                  });
                },
              ),
              SizedBox(
                height: 10,
              ),
              TextField(
                controller: _oath,
                keyboardAppearance: brightnessValue,
                decoration: InputDecoration(
                    border: OutlineInputBorder(), labelText: '宣言'),
                onChanged: (value) {
                  setState(() {
                    _oathData = value;
                  });
                },
              ),
              SizedBox(height: 10,),
              Container(
                decoration: BoxDecoration(
                    border: Border.all(width: 1,color: Colors.grey),
                    borderRadius: BorderRadius.circular(4)
                ),
                height: 60,
                child: Row(
//                mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    SizedBox(width: 10,),
                    Text('是否完成',style: TextStyle(fontSize: 16,color: _textColor),),
                    SizedBox(width: 20,),
                    Expanded(
                      flex: 1,
                      child: RaisedButton(
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(10),
                        ),
                        color: _finishedColor,
                        child: Text('已完成',
                            style: TextStyle(fontSize: 20, color: Colors.black54)),
                        onPressed: () {
                          setState(() {
                            _finishData = 'yes';
                            _finishedColor = Colors.lightBlue;
                            _unFinishColor = Colors.white54;
                          });
                        },
                      ),
                    ),
                    SizedBox(width: 30,),
                    Expanded(
                      flex: 1,
                      child: RaisedButton(
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(10),
                        ),
                        color: _unFinishColor,
                        child: Text('未完成',
                            style: TextStyle(fontSize: 20, color: Colors.black54)),
                        onPressed: () {
                          setState(() {
                            _finishData = 'no';
                            _unFinishColor = Colors.lightBlue;
                            _finishedColor = Colors.white54;
                          });
                        },
                      ),
                    ),
                    SizedBox(width: 20,)

                  ],
                ),
              ),
              SizedBox(height: 20,),
              Container(
                width: 150,
                height: 49,
                child: RaisedButton(
                    color: Colors.lightBlueAccent,
                    textColor: Colors.white,
                    child: Text('删除',style: TextStyle(fontSize: 20),),
                    onPressed: _delete
                ),
              )
            ],
          ),
          scrollDirection: Axis.vertical,
        ),
      ),
    );
  }
}